var mysql = require('mysql');
var publictool = require("../publictool/publictool");

exports.start = function () {

    var client = getclient();

    client.query('create table if not exists user ' +
        '(account VARCHAR(255), ' +
        'password VARCHAR(255), ' +
        'lever VARCHAR(255), ' +
        'start_timestamp VARCHAR(255), ' +
        'end_timestamp VARCHAR(255), ' +
        'company_name VARCHAR(255), ' +
        'name VARCHAR(255), ' +
        'tel VARCHAR(255), ' +
        'qq VARCHAR(255), ' +
        'accesstoken VARCHAR(255), ' +
        'isuse CHAR(1)) ');

    client.query('create table if not exists device ' +
        '(manager VARCHAR(255), ' +
        'sn VARCHAR(100) unique) ');

    client.query('create table if not exists productInfo ' +
        '(bundleid VARCHAR(255), ' +
        'productid VARCHAR(100) unique, ' +
        'price VARCHAR(255), ' +
        'des VARCHAR(255)) ');

    client.query('create table if not exists receipt  ' +
        '(bundleid VARCHAR(255), ' +
        'productid VARCHAR(255), ' +
        'price VARCHAR(255), ' +
        'account VARCHAR(255), ' +
        'sn VARCHAR(255), ' +
        'receipt TEXT, ' +
        'timestamp VARCHAR(255), ' +
        'status CHAR(1), ' +
        'uuid VARCHAR(255), ' +
        'idfv VARCHAR(255), ' +
        'msg VARCHAR(255), ' +
        'des VARCHAR(255), ' +
        'product_index INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT)AUTO_INCREMENT = 200000001; '
    );
    client.end();
}

function getclient() {
    var client = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'root111111',
        database: 'Stock',
    });
    client.connect();

    return client;
}

exports.checkuser = function (client, res, ischeck, accesstoken) {
    var p = new Promise(function (resolve, reject) {

        if (!accesstoken) {
            publictool.returnErr(res, '无accesstoken参数', 1);
            client.end();
            return;
        }

        var sql = 'SELECT lever,account FROM user where accesstoken=?';
        var addSqlParams = [accesstoken];
        client.query(sql, addSqlParams, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                publictool.returnErr(res, '查询参数错误', 1);
                client.end();
                return;
            }

            if (result.length == 0) {
                publictool.returnErr(res, '无此用户', 6);
                client.end();
                return;
            }

            var resultvalue = result[0];
            if (!ischeck) {
                resolve(resultvalue);
                return;
            }


            if (resultvalue.lever != 'Root') {
                publictool.returnErr(res, 'User账号无权限修改', 3);
                client.end();
                return;
            }

            resolve(resultvalue);
        });
    });
    return p;
}

exports.checkusernum = function () {
    var p = new Promise(function (resolve, reject) {
        var client = getclient();
        var sql = 'SELECT * FROM user';
        client.query(sql, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                client.end();
                resolve(true);
                return;
            }

            client.end();
            if(result.length > 30){
                resolve(false);
            }else{
                resolve(true);
            }
        });
    });

    return p;
}

exports.checksnnum = function () {
    var p = new Promise(function (resolve, reject) {
        var client = getclient();
        var sql = 'SELECT * FROM device';
        client.query(sql, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                client.end();
                resolve(true);
                return;
            }

            client.end();
            if(result.length > 30){
                resolve(false);
            }else{
                resolve(true);
            }
        });
    });

    return p;
}

exports.checkReceiptNum = function () {
    var p = new Promise(function (resolve, reject) {
        var client = getclient();
        var sql = 'SELECT * FROM receipt where status = ?';
        var addSqlParams = ['0'];
        client.query(sql,addSqlParams, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                client.end();
                resolve(true);
                return;
            }

            client.end();
            if(result.length > 500){
                resolve(false);
            }else{
                resolve(true);
            }
        });
    });

    return p;
}

exports.client = getclient;